﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WmsApp.StaticData
{

    public class GoodsCodeTypeSubject : IGoodsCodeTypeSubject
    {
        private string _action = string.Empty;
        private readonly IList<BaseGoodsCodeTypeObserver> _abservers = new List<BaseGoodsCodeTypeObserver>();
        public void Attach(BaseGoodsCodeTypeObserver observer)
        {
            _abservers.Add(observer);
            observer.ParentControlClosed += (Object sender, EventArgs e) => Detach(observer);
        }

        public void Detach(BaseGoodsCodeTypeObserver observer)
        {
            _abservers.Remove(observer);
        }

        public void Nofify()
        {
            Nofify(true);
        }

        public void Nofify(bool refreshFromServer)
        {
            if (refreshFromServer)
            {
                StaticObjects.RefreshStaticObjects();
            }
            foreach (var baseGoodsCodeTypeObserver in _abservers)
            {
                if (baseGoodsCodeTypeObserver != null)
                {
                    baseGoodsCodeTypeObserver.UpdateData();
                }
            }
        }

        public string SubjectState
        {
            get
            {
                return _action;
            }
            set
            {
                _action = value;
            }
        }
    }
}
